package contextx

import (
	"sieve_admin_server_userpass/app/server"
	"strconv"
)

type Var struct {
	Variable string
	IsNumber bool
}

func NewVar(v string, b ...bool) Var {

	vv := Var{
		Variable: v,
	}
	if len(b) > 0 {
		if len(b) == 1 {
			vv.IsNumber = b[0]
		}
	}

	return vv
}

func GetRequestArgs(ctx *server.Context, keys []Var) map[string]interface{} {
	var (
		addKeys = keys
		args    = map[string]interface{}{}
	)

	for _, v := range addKeys {
		if ctx.Args.Has(v.Variable) {

			vv := ctx.Args.Get(v.Variable)
			if vv == "" {
				continue
			}
			args[v.Variable] = vv
			if v.IsNumber {
				args[v.Variable], _ = strconv.ParseUint(vv, 10, 64)
				continue
			}
		}
	}
	return args
}
